clear all
set more off, perm
local mydir "`1'"
global Tables	`mydir'/XS/Tables
global Work	`mydir'/XS/DataWork
global Raw	`mydir'/XS/DataRaw
global WRDS	`mydir'/WRDS
/* ********************************************************************* */
use if year(date)>1962 using $Work/FamaFrench02, clear
drop SIZE BOOK PROF INVE
* Size x BM: The portfolios for July of year t to June of t+1 include all NYSE, AMEX, and NASDAQ stocks for which we have market equity data for December of t-1 and June of t, and (positive) book equity data for t-1.

generate rSIZE=(mcap_size>0 & mcap_book>0) & (bookeq>0) & (bm+mcap_size+mcap_size+ret1<.) & (count>1)
generate rBOOK=rSIZE

* Profitability:  All NYSE, AMEX, and NASDAQ stocks for which we have market equity data for June of t, (positive) book equity data for t-1, non-missing revenues data for t-1, and non-missing data for at least one of the following: cost of goods sold, selling, general and administrative expenses, or interest expense for t-1
generate rPROF=(rBOOK==1) & (op<.) & (count>1)

*INV: All NYSE, AMEX, and NASDAQ stocks for which we have market equity data for June of t and total assets data for t-2 and t-1
generate rINVE=(mcap_book>0 & mcap_book<.) & (inv<.) & (count>1)

bys date: astile SIZE = mcap_size if rSIZE==1, qc(exchcd==1) nq(10)
bys date: astile BOOK = bm 	  if rBOOK==1, qc(exchcd==1) nq(10)
bys date: astile PROF = op        if rPROF==1, qc(exchcd==1) nq(10)
bys date: astile INVE = inv       if rINVE==1, qc(exchcd==1) nq(10)
* ******************************
generate S=1 if SIZE>=1 & SIZE<=05
replace  S=0 if SIZE>=6 & SIZE<=10

generate H=0.0 if BOOK>=1 & BOOK<=03
replace  H=0.5 if BOOK>=4 & BOOK<=07
replace  H=1.0 if BOOK>=8 & BOOK<=10

egen id=group(permco permno)
tsset id datem
* ***********************
save $Work/FamaFrench04, replace
